/* $Id: Footer.java 83 2007-12-19 06:48:54Z osborn.steven $
 * 
 * Undroid Package Manager
 *
 * Copyright 2007 Steven Osborn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.bitsetters.undroid.common;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.bitsetters.undroid.CategoryListActivity;
import com.bitsetters.undroid.NewsActivity;
import com.bitsetters.undroid.R;
import com.bitsetters.undroid.RepoListActivity;
import com.bitsetters.undroid.UninstallActivity;
import com.bitsetters.undroid.UpgradeActivity;

/**
 * Common footer.
 * 
 * @author Steven Osborn - http://steven.bitsetters.com
 */
public class Footer {

    // sorta UID's for buttons
    public static final int INSTALL_ID = 101;
    public static final int UNINSTALL_ID = 102;
    public static final int UPGRADE_ID = 103;
    public static final int REPO_ID = 104;
    public static final int NEWS_ID = 105;

    //button references
    private static ImageButton installIcon;
    private static ImageButton removeIcon;
    private static ImageButton updateIcon;
    private static ImageButton repoIcon;
    private static ImageButton newsIcon;

    public static Activity currentActivity;

    /**
     * 
     * @param a
     */
    public static void setup(Activity a){
        currentActivity = a;
        
        // Install 
        installIcon = (ImageButton) a.findViewById(INSTALL_ID);
        installIcon.setOnFocusChangeListener(footerFocusListener);
        installIcon.setOnClickListener(installClickListener);

        // Remove 
        removeIcon = (ImageButton) a.findViewById(UNINSTALL_ID);
        removeIcon.setOnFocusChangeListener(footerFocusListener);
        removeIcon.setOnClickListener(removeClickListener);

        // Update
        updateIcon = (ImageButton) a.findViewById(UPGRADE_ID);
        updateIcon.setOnFocusChangeListener(footerFocusListener);
        updateIcon.setOnClickListener(updateClickListener);

        // Repository
        repoIcon = (ImageButton) a.findViewById(REPO_ID);
        repoIcon.setOnFocusChangeListener(footerFocusListener);
        repoIcon.setOnClickListener(repoClickListener);
        
        // News
        newsIcon = (ImageButton) a.findViewById(NEWS_ID);
        newsIcon.setOnFocusChangeListener(footerFocusListener);
        newsIcon.setOnClickListener(newsClickListener);

        String selected = currentActivity.getLocalClassName();


        if(selected.compareTo("CategoryActivity") == 0 ||
                selected.compareTo("ProgramDetailsActivity") == 0 ||
                selected.compareTo("ProgramListActivity") == 0) {
            installIcon.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.install_selected22));
        } else {
            installIcon.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.install22));

        }

        if(selected.compareTo("UninstallActivity") == 0) {
            removeIcon.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.uninstall_selected22));
        } else {
            removeIcon.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.uninstall22));

        }

        if(selected.compareTo("UpgradeActivity") == 0) {
            updateIcon.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.upgrade_selected22));
        } else {
            updateIcon.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.upgrade22));

        }

        if(selected.compareTo("RepoActivity") == 0) {
            repoIcon.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.repos_selected22));
        } else {
            repoIcon.setImageDrawable(a.getResources()
                    .getDrawable(R.drawable.repos22));

        }
        
        if(selected.compareTo("News") == 0) {
            newsIcon.setImageDrawable(a.getResources()
        	    .getDrawable(R.drawable.news_selected22));
        } else {
            newsIcon.setImageDrawable(a.getResources()
        	    .getDrawable(R.drawable.news22));
        }

    }

    
    /**
     * 
     */
    private static ImageButton.OnClickListener newsClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Footer.currentActivity, NewsActivity.class);
            Footer.currentActivity.startActivity(i);
            Footer.currentActivity.finish();
        }
    };

    
    /**
     * 
     */
    private static ImageButton.OnClickListener repoClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Footer.currentActivity, RepoListActivity.class);
            Footer.currentActivity.startActivity(i);
            Footer.currentActivity.finish();
        }
    };

    /**
     * 
     */
    private static ImageButton.OnClickListener installClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Footer.currentActivity, CategoryListActivity.class);
            Footer.currentActivity.startActivity(i);
            Footer.currentActivity.finish();
        }
    };

    /**
     * 
     */
    private static ImageButton.OnClickListener removeClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Footer.currentActivity, UninstallActivity.class);
            Footer.currentActivity.startActivity(i);
            Footer.currentActivity.finish();
        }
    };

    /**
     * 
     */
    private static ImageButton.OnClickListener updateClickListener = 
        new ImageButton.OnClickListener() {

        public void onClick(View arg0) {
            Intent i = new Intent();
            i.setClass(Footer.currentActivity, UpgradeActivity.class);
            Footer.currentActivity.startActivity(i);
            Footer.currentActivity.finish();
        }
    };


    /**
     * 
     */
     private static ImageButton.OnFocusChangeListener footerFocusListener =
         new ImageButton.OnFocusChangeListener() {

         public void onFocusChanged(View arg0, boolean arg1) {
             if(arg1) {
                 arg0.setBackground(R.drawable.background2);
             } else {
                 arg0.setBackground(R.drawable.background);
             }
         }
     };

     /**
      * 
      * @author Steven Osborn - http://steven.bitsetters.com
      */
     public static class CommonFooterView extends TableLayout {

         public CommonFooterView(Context context, AttributeSet attrs,
                 Map<Object, Object> inflateParams) {

             super(context, attrs, inflateParams);

             // Create row container
             TableRow row = new TableRow(context);

             // Create buttons
             ImageButton installButton = new ImageButton(context);
             ImageButton uninstallButton = new ImageButton(context);
             ImageButton upgradeButton = new ImageButton(context);
             ImageButton repoButton = new ImageButton(context);
             ImageButton newsButton = new ImageButton(context);

             // Give buttons UID's (sorta)
             installButton.setId(INSTALL_ID);
             uninstallButton.setId(UNINSTALL_ID);
             upgradeButton.setId(UPGRADE_ID);
             repoButton.setId(REPO_ID);
             newsButton.setId(NEWS_ID);

             // Set button images
             installButton.setImageDrawable(getResources()
                     .getDrawable(R.drawable.install22));
             uninstallButton.setImageDrawable(getResources()
                     .getDrawable(R.drawable.uninstall22));
             upgradeButton.setImageDrawable(getResources()
                     .getDrawable(R.drawable.upgrade22));
             repoButton.setImageDrawable(getResources()
                     .getDrawable(R.drawable.repos22));
             newsButton.setImageDrawable(getResources()
        	     .getDrawable(R.drawable.news22));

             // Set Padding l,t,r,b
             installButton.setPadding(0, 3, 0, 3);
             uninstallButton.setPadding(0, 3, 0, 3);
             upgradeButton.setPadding(0, 3, 0, 3);
             repoButton.setPadding(0, 3, 0, 3);
             newsButton.setPadding(0, 3, 0, 3);


             // Set Background Image
             installButton.setBackground(getResources()
                     .getDrawable(R.drawable.background));
             uninstallButton.setBackground(getResources()
                     .getDrawable(R.drawable.background));
             upgradeButton.setBackground(getResources()
                     .getDrawable(R.drawable.background));
             repoButton.setBackground(getResources()
                     .getDrawable(R.drawable.background));
             newsButton.setBackground(getResources()
                     .getDrawable(R.drawable.background));


             // Add buttons to row

             row.addView(installButton, new TableRow.LayoutParams(
                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             row.addView(uninstallButton, new TableRow.LayoutParams(
                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             row.addView(upgradeButton, new TableRow.LayoutParams(
                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             row.addView(repoButton, new TableRow.LayoutParams(
                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             row.addView(newsButton, new TableRow.LayoutParams(
                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

             
             // Make columns expand to fill space
             setColumnStretchable(0, true);
             setColumnStretchable(1, true);
             setColumnStretchable(2, true);
             setColumnStretchable(3, true);
             setColumnStretchable(4, true);

             // Set row background color
             row.setBackground(getResources().getDrawable(R.drawable.background));

             // Add row to table
             addView(row, new TableLayout.LayoutParams(
                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
         }


     }
}
